package com.atguigu.EdumemberController;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.OrderVo.OrderMember;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.entity.UcenterMember;
import com.atguigu.entity.vo.loginVo;
import com.atguigu.entity.vo.registerVo;
import com.atguigu.service.impl.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;


/**
 * @author cyt
 * @create 2020-05-15 11:51
 */
@RestController
@RequestMapping("/ucenterservice/apimember")
public class MerberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    // 返回订单需要的会员信息
    @GetMapping("/getOrderMember/{id}")
    public OrderMember getOrderMember(@PathVariable("id") String id) {
        UcenterMember member = ucenterMemberService.getById(id);
        OrderMember orderMember = new OrderMember();
        BeanUtils.copyProperties(member, orderMember);
        return orderMember;
    }

    // 返回评论需要的会员信息
    @GetMapping("getInfoUc/{id}")
    public UcenterMember getInfoUc(@PathVariable("id") String id) {
        UcenterMember member = ucenterMemberService.getById(id);
        return member;
    }

    /*
    登陆
     */
    @PostMapping("/login")
    public R login(@RequestBody loginVo loginVo) {
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token", token);

    }

    /*
    注册
     */
    @PostMapping("/register")
    public R login(@RequestBody registerVo vo) {
        ucenterMemberService.register(vo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember member = ucenterMemberService.getLoginInfo(memberId);
            return R.ok().data("member", member);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "error");
        }
    }

    // 统计出每天多少人 的注册人数
    @GetMapping("/getRegisterNumByDate/{day}")
    public R getRegisterNumByDate(@PathVariable("day") String day) {
        Integer count = ucenterMemberService.selectRegisterCount(day);
        return R.ok().data("num",count);
    }


}
